package com.lg.partition28;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/3/25 10:44
 */
public class P2722总分ScoreInflation {

    private static final int N = 10010;
    private static int[] f = new int[N];
    private static int n, m;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split("\\s+");
        m = Integer.parseInt(ss[0]);
        n = Integer.parseInt(ss[1]);
        for (int i = 0; i < n; i++) {
            ss = reader.readLine().split(" ");
            int w = Integer.parseInt(ss[0]), v = Integer.parseInt(ss[1]);
            for (int j = v; j <= m; j++) {
                f[j] = Math.max(f[j], f[j - v] + w);
            }
        }
        writer.write(f[m] + "");
        writer.flush();
    }
}
